package cn.wggf.dev.qcgg.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;





import org.apache.log4j.Logger;


import com.mysql.jdbc.PreparedStatement;

import cn.wggf.dev.qcgg.dao.QcggDao;
import cn.wggf.dev.qcgg.pojo.QcggZp;
import cn.wggf.dev.qcgg.util.SqlService;


/**
 * 汽车公告dao接口实现类
 * @author Yubo Jia
 *
 * 2018年1月26日上午9:20:46
 */
public class QcggDaoImpl implements QcggDao {
	
	private Logger logger = Logger.getLogger(QcggDaoImpl.class);

	/**
	 * 根据品牌和公告型号查询汽车公告
	 */
	public List<Map<String, Object>> getQcggByConditions(String PP ,String GGXH) {
		SqlService sqlService = new SqlService();
		StringBuffer sb = new StringBuffer();
		Object[] params = new Object[1];
		sb.append("select * from qcgg where 1=1 ");
		String sql = null;
		if (PP!=null && !"".equals(PP)) {
			sb.append(" and PP like \"%\"?\"%\" ");
			params[0] = PP;
			if (GGXH != null && !"".equals(GGXH)) {
				sb.append(" and GGXH = ? ");
				params = new Object[2];
				params[0] = PP;
				params[1] = GGXH;
			}
		} else {
			if (GGXH!=null && !"".equals(GGXH)) {
				sb.append(" and GGXH = ? ");
				params= new Object[1];
				params[0] = GGXH;
			} else {
				params = null;
			}
		}
		sb.append(" limit 0,20 ");
		sql = sb.toString();
		logger.info("执行的sql语句是："+sql);
		if (params!=null) {
			for (int i = 0; i < params.length; i++) {
				logger.info("传入的参数为："+params[i]);
			}
		}
		List<Map<String, Object>> qcggList = sqlService.execQuery(sql, params);
		logger.info("查询结果共："+qcggList.size());
		return qcggList;
	}
	/**
	 * 
	 */
	public List<Map<String, Object>>  getQcggById(Integer ID) {
		System.out.println("执行>>>>>>>>>>>>>>>>>>>>>>>");
		// TODO Auto-generated method stub    
		SqlService sqlService = new SqlService();
		String sql = "select GGXH,GGPC,ZP1,ZP2,ZP3,ZP4,ZP5,ZP6,ID from qcgg where ID=?";
		Object[] params={ID};
		logger.info("执行的sql语句是11："+sql);
		List<Map<String, Object>> rs=sqlService.select(sql, params);
	 //  System.out.println("获得结果为"+rs.get(0));
	  
		return rs;
	}
	public int updateQcggById(Integer ID,List<QcggZp> list) {
		String ZP1=null;
		String ZP2=null;
		String ZP3=null;
		String ZP4=null;
		String ZP5=null;
		String ZP6=null;
		for (QcggZp qcggZp : list) {
			if (qcggZp.getKey().equals("zp1")) {
				ZP1=qcggZp.getPath();
			}else if (qcggZp.getKey().equals("zp2")) {
				ZP2=qcggZp.getPath();
			}else if (qcggZp.getKey().equals("zp3")) {
				ZP3=qcggZp.getPath();
			}else if (qcggZp.getKey().equals("zp4")) {
				ZP4=qcggZp.getPath();
			}else if (qcggZp.getKey().equals("zp5")) {
				ZP5=qcggZp.getPath();
			}else if (qcggZp.getKey().equals("zp6")) {
				ZP6=qcggZp.getPath();
			}
		}
		// TODO Auto-generated method stub
		SqlService sqlService = new SqlService();
		String sql = "update qcgg set ZP1=?,ZP2=?,ZP3=?,ZP4=?,ZP5=?,ZP6=? where ID=?";
		Object[] params={ZP1,ZP2,ZP3,ZP4,ZP5,ZP6,ID};
		int i=sqlService.execUpdate(sql, params);
		
		return i;
	}
}
